/**
 * Created by Soul on 2016/8/29.
 */


    var assert = require("assert");

    var mongoose = require('mongoose');
    mongoose.connect('mongodb://localhost/mongoose');

    //
    // Schema define the collection structure
    //
    var Schema = mongoose.Schema;
    var animalSchema = new Schema({

        //
        // Here we define the app schema
        // schema types listed below:
        // String
        // Number
        // Date
        // Buffer
        // Boolean
        // Mixed
        // Objectid
        // Array
        //

        name:   String,
        type:   String,
        age:    Number,
        sex:    Number,
        ciday:  Date,
        data: { bday: Date, height: Number, weight: Number },
        adopted: Boolean
    });

    //animalSchema.methods.findByType = function(found_cb){
    //    return this.model('Animal').find({type: this.type}, found_cb);
    //};
    animalSchema.statics.findByType = function(type, found_cb){
        return this.find({type: type}, found_cb);
    };

    var dt = require("../utils/datetime.js");
    //
    // Model: class type of document
    // Document: stands for one record of document, stands for a model class instance
    //
    // some schema type, mongoose can detect its modification automatically,
    // use  doc.markModified('pathToYourDate')  to notify
    //
    var Animal = mongoose.model('Animal', animalSchema);

    //var dog = new Animal({
    //    name: 'lucy',
    //    type: 'dog',
    //    age:    2,
    //    sex:    0, // 0 : female, 1: male
    //    ciday: dt.dt_utls.time_from_yMd(2015, 10, 1),
    //    data: {
    //        bday: dt.dt_utls.time_from_yMd(2013, 10, 1),
    //        height: 60,
    //        weight: 10
    //    },
    //    adopted: false
    //});
    //
    //dog.save();

    Animal.findByType("dog", function(err, dogs) {
        if (err){
            console.log(err);
            return
        }
        console.log("Find " + dogs.length + " dogs");
        if(dogs.length)
        {
            var dog1 = dogs[0];
            console.log("Find " + dog1.name);
            //console.log("Update " + dog1.name + " to lucy");
            //dog1.name = "lucy";
            //var promise = dog1.save();
            ////assert.ok(promise instanceof require('mpromise'));
            //
            //promise.then(function (doc) {
            //    mongoose.disconnect();
            //});

            console.log("Delete " + dog1.name);
            dog1.remove(function (err, result) {
                mongoose.disconnect();
            });

            //Animal.update({ name: dog1.name }, { $set: { adopted: true }}, function (err, raw) {
            //    if (err)
            //    {
            //        console.log(err);
            //        return;
            //    }
            //    console.log('The raw response from Mongo was ', raw);
            //
            //    mongoose.disconnect();
            //});
        }else{
            mongoose.disconnect();
        }
    });